﻿@{
	Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
	<input type="hidden" name="id" id="id" class="layui-input" />
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">车间编码</label>
			<div class="layui-input-inline layui-input-wrap">
				<input type="text" id="workshopCode" name="workshopCode" lay-verify="required" placeholder="请输入车间编码" class="layui-input">
			</div>
			<div class="layui-form-mid">
				<input type="checkbox" lay-skin="switch" lay-filter="switchTest">自动生成
			</div>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">车间名称</label>
		<div class="layui-input-block">
			<input type="text" id="workshopName" name="workshopName" lay-verify="required" placeholder="请输入车间名称" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item layui-form-text">
		<label class="layui-form-label">面积</label>
		<div class="layui-input-block">
			<input type="number" id="area" name="area" lay-verify="required" placeholder="请输入车间名称" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">负责人</label>
		<div class="layui-input-block">
			<input type="text" id="head" name="head" lay-verify="required" placeholder="请输入负责人" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">是否启用</label>
		<div class="layui-input-block">
			<input type="radio" id="state" name="state" value="1" title="是" checked>
			<input type="radio" id="state" name="state" value="2" title="否">
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-input-block">
			<button type="submit" class="layui-btn" lay-submit lay-filter="upd">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
<script>
	$(function(){
		GetbyidWorks()
	})

	//反填车间设置信息
	function GetbyidWorks(){
		$.ajax({
			url:'/Puffs/SelectByidWork',
			type:'get',
			data:{
				id:@ViewBag.id
			},
			dataType:'json',
			success:function(res){
				$("#id").val(res.data.id);
				$("#workshopCode").val(res.data.workshopCode)
				$("#workshopName").val(res.data.workshopName)
				$("#area").val(res.data.area)
				$("#head").val(res.data.head)
				$("[name='state'][value=" + res.data.state + "]").prop("checked", true);
				layui.form.render("radio");
			}
		})
	}

	layui.use(['form'],function(){
		var form = layui.form;
		var layer = layui.layer;
		// 指定开关事件,生成车间编码
		form.on('switch(switchTest)', function(data){
			if(data.elem.checked){
				var random=Math.floor(Math.random()*10)+'';
				for(var i=0;i<2;i++){
					random+=Math.floor(Math.random()*10);
				 }
				 random='WS'+random;
				 $("#workshopCode").val(random);
				 }else{
					 $('input[name="workshopCode"]').val('');
				 }
		});

		// 提交事件修改
		form.on('submit(upd)', function (data) {
			var field = data.field; // 获取表单字段值
			field.id=$("#id").val();
			$.ajax({
				url: "/Puffs/UpdateWorkdatas",
				type: "post",
				async: false,
				data: field,
				datatype: "json",
				success: function (res) {
					console.log(res.data)
					if (res.code == 0) {
						layer.alert("修改成功！");
						closePage()
					}else{
						layer.alert('修改失败')
					}
				}
			});
			return false; // 阻止默认 form 跳转
		});
	})

	function closePage(){
		var index = parent.layer.getFrameIndex(window.name); // 获取当前 iframe 层的索引
		parent.layer.close(index); // 关闭当前 iframe 层
		parent.location.reload(); // 刷新父页面
	}
</script>
